<?php

class Register extends Controller {
	
	function Register()
	{
		parent::Controller();
		$this->load->model('Member_model');
	}
	
	function index() {
		$data['inc_script'] = '';
		$this->load->view('register_option_view',$data);
	}
	
	function par() {
		//get value from form
		$member_info = array(
			'email' => $this->input->post('tf_email'),
			'password' => dohash($this->input->post('tf_password')),
			'member_type' => 1,
			'date_created' => date('d-m-Y'),
		);
		//echo $member_info['password'];
		//exit;
		$member_par_info = array(
			'firstname' => $this->input->post('tf_firstname'),
			'lastname' => $this->input->post('tf_lastname'),
			'birthday' => date_mysql($this->input->post('tf_birthday')),
			'gender' => $this->input->post('sl_gender'),
			'address' => $this->input->post('tf_address'),
			'phone' => $this->input->post('tf_phone'),
			'country_id' => $this->input->post('sl_country_id'),
			'city_id' => $this->input->post('sl_city_id'),
			'district_id' => $this->input->post('sl_district_id'),
		);
		
		$form_info = array_merge($member_info, $member_par_info);
		
		if ($this->input->post('btn_register')) {
			//set form validation
			$form_validation = array(
				array('field' => 'tf_firstname','label' => 'lang:firstname','rules' => 'required|max_length[45]'),
				array('field' => 'tf_lastname','label' => 'lang:lastname','rules' => 'required|max_length[45]'),
				array('field' => 'tf_birthday','label' => 'lang:birthday','rules' => 'required|max_length[45]'),
				array('field' => 'tf_email','label' => 'lang:email','rules' => 'required|max_length[45]|valid_email'),
				array('field' => 'tf_confirm_email','label' => 'lang:confirm email','rules' => 'required|max_length[45]|matches[tf_email]'),
				array('field' => 'tf_password','label' => 'lang:password','rules' => 'required|max_length[45]'),
				array('field' => 'tf_confirm_password','label' => 'lang:confirm password','rules' => 'required|max_length[45]|matches[tf_password]'),
				array('field' => 'sl_gender','label' => 'lang:email','rules' => 'required|max_length[45]'),
				array('field' => 'tf_phone','label' => 'lang:phone','rules' => 'required|max_length[45]'),
				array('field' => 'tf_address','label' => 'lang:address','rules' => 'required|max_length[45]'),
				array('field' => 'sl_country_id','label' => 'lang:country','rules' => 'required|max_length[45]'),
				array('field' => 'sl_city_id','label' => 'lang:city','rules' => 'required|max_length[45]'),
				array('field' => 'sl_district_id','label' => 'lang:district','rules' => 'required|max_length[45]'),
			);
			$this->form_validation->set_rules($form_validation);
			
			//--check form validation
			if ($this->form_validation->run()==true) {
				if ($_FILES['f_display_image']['name']!='') {
					$config['upload_path'] = BASEPATH."../images/upload/";
					$config['allowed_types'] = 'gif|jpg|png';
					$config['file_name'] = $this->lang->line('site_name') .'-'. $_FILES['f_display_image']['name'];
					$this->load->library('upload',$config);
					
					//--do upload
					if ($this->upload->do_upload('f_display_image')) {
						$fileuploaded_info = $this->upload->data();
						$member_par_info['display_image'] = $fileuploaded_info['file_name']; 
					} else {
						$this->upload->display_errors();
					}
				}
				
				//--insert into Member table and return id
				$member_id = $this->Member_model->addMember($member_info);
				$member_par_info['member_id'] = $member_id;
				$this->Member_model->addMember_par($member_par_info);
				redirect('register/success');	
			}
		}
		
		$data['inc_script'] = '';
		$data['form_info'] = $form_info;
		$this->load->view('register_par_view',$data);
	}
	
	function pro() {
		//get value from form
		$member_info = array(
			'email' => $this->input->post('tf_email'),
			'password' => dohash($this->input->post('tf_password')),
			'member_type' => 2,
			'date_created' => date('d-m-Y'),
		);
		
		$member_pro_info = array (
			'company_name' => $this->input->post('tf_company_name'),
			'address' => $this->input->post('tf_address'),
			'country_id' => $this->input->post('sl_country_id'),
			'city_id' => $this->input->post('sl_city_id'),
			'district_id' => $this->input->post('sl_district_id'),
			'phone' => $this->input->post('tf_phone'),
			'intro_content' => $this->input->post('ta_intro_content'),
			'website' => $this->input->post('tf_website'),
		);
		
		$form_info = array_merge($member_info, $member_pro_info);
		
		if ($this->input->post('btn_register')) {
			//set form validation
			$form_validation = array(
				array('field' => 'tf_company_name','label' => 'lang:company_name','rules' => 'required|max_length[45]'),
				array('field' => 'tf_address','label' => 'lang:address','rules' => 'required|max_length[45]'),
				array('field' => 'tf_email','label' => 'lang:email','rules' => 'required|max_length[45]|valid_email'),
				array('field' => 'tf_confirm_email','label' => 'lang:confirm email','rules' => 'required|max_length[45]|matches[tf_email]'),
				array('field' => 'tf_password','label' => 'lang:password','rules' => 'required|max_length[45]'),
				array('field' => 'tf_confirm_password','label' => 'lang:confirm password','rules' => 'required|max_length[45]|matches[tf_password]'),
				array('field' => 'tf_phone','label' => 'lang:phone','rules' => 'required|max_length[45]'),
				array('field' => 'sl_country_id','label' => 'lang:country','rules' => 'required|max_length[45]'),
				array('field' => 'sl_city_id','label' => 'lang:city','rules' => 'required|max_length[45]'),
				array('field' => 'sl_district_id','label' => 'lang:district','rules' => 'required|max_length[45]'),
				array('field' => 'ta_intro_content','label' => 'lang:intro_content','rules' => 'required|max_length[45]'),
				array('field' => 'tf_website','label' => 'lang:website','rules' => 'required|max_length[45]'),
			);
			$this->form_validation->set_rules($form_validation);
			
			//--check form validation
			if ($this->form_validation->run()==true) {
				if ($_FILES['f_display_image']['name']!='') {
					$config['upload_path'] = BASEPATH."../images/upload/";
					$config['allowed_types'] = 'gif|jpg|png';
					$config['file_name'] = $this->lang->line('site_name') .'-'. $_FILES['f_display_image']['name'];
					$this->load->library('upload',$config);
					
					//--do upload
					if ($this->upload->do_upload('f_display_image')) {
						$fileuploaded_info = $this->upload->data();
						$member_pro_info['display_image'] = $fileuploaded_info['file_name']; 
					} else {
						$this->upload->display_errors();
					}
				}
				
				//--insert into Member table and return id
				$member_id = $this->Member_model->addMember($member_info);
				$member_pro_info['member_id'] = $member_id;
				$this->Member_model->addMember_pro($member_pro_info);
				redirect('register/success');	
			}
		}
		
		$data['inc_script'] = '';
		$data['form_info'] = $form_info;
		$this->load->view('register_pro_view',$data);
	}
	
	function success() {
		$data['inc_script'] = '';
		$this->load->view('register_success_view',$data);
	}
}